﻿using LogCenterService.Models;
using SqlSugar;

namespace LogCenterService.Common.Extension;

/// <summary>
/// 查询扩展
/// </summary>
public static class QueryableExtension
{
    /// <summary>
    /// 分页查询
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="sugarQueryable"></param>
    /// <param name="page"></param>
    /// <returns></returns>
    public static async Task<TableModel<T>> PageListAsync<T>(this ISugarQueryable<T> sugarQueryable, Models.PageModel page)
    {
        RefAsync<int> totalCount = 0;
        var data = await sugarQueryable.ToPageListAsync(page.Page, page.Limit, totalCount);
        return new TableModel<T>
        {
            TotalCount = totalCount,
            PageCount = (int)Math.Ceiling((double)totalCount / page.Limit),
            Data = data
        };
    }
}